package com.simple.datax.bo.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.simple.datax.bo.IDataxSelfNodePropBO;
import com.simple.datax.dao.IDataxSelfNodePropDAO;
import com.simple.datax.vo.DataxSelfNodePropVO;

@Service("dataxSelfNodePropBO")
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public class DataxSelfNodePropBOImpl implements IDataxSelfNodePropBO {

	@Autowired
	private IDataxSelfNodePropDAO dataxSelfNodePropDAO;

	@Override
	public DataxSelfNodePropVO findById(String fcNode) {
		return dataxSelfNodePropDAO.findById(fcNode);
	}

	@Override
	public List<DataxSelfNodePropVO> findAll() {
		return dataxSelfNodePropDAO.findAll();
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false,rollbackFor={Exception.class})
	public void update(String fcNodeSelect, String fcNodeDescSelect,
			String fcSendTypeSelect, String fcSendParamSelect,
			String fcRecvTypeSelect, String fcRecvParamSelect,
			String fcMsgFormateId, String fcMsgTypeSelect,
			String fcMsgCharsetSelect) {
		DataxSelfNodePropVO dataxSelfNodePropVO = dataxSelfNodePropDAO.findById(fcNodeSelect);
		if (dataxSelfNodePropVO == null) {
			dataxSelfNodePropVO = new DataxSelfNodePropVO();
			dataxSelfNodePropVO.setFcNode(fcNodeSelect);
		}
		dataxSelfNodePropVO.setFcNodeDesc(fcNodeDescSelect);
		dataxSelfNodePropVO.setFcCommunSendType(fcSendTypeSelect);
		dataxSelfNodePropVO.setFcCommunSendParam(fcSendParamSelect);
		dataxSelfNodePropVO.setFcCommunRecvType(fcRecvTypeSelect);
		dataxSelfNodePropVO.setFcCommunRecvParam(fcRecvParamSelect);
		dataxSelfNodePropVO.setFcMsgFormateId(fcMsgFormateId);
		dataxSelfNodePropVO.setFcMsgType(fcMsgTypeSelect);
		dataxSelfNodePropVO.setFcMsgCharset(fcMsgCharsetSelect);
		
		dataxSelfNodePropDAO.saveOrUpdate(dataxSelfNodePropVO);
	}

	@Override
	public DataxSelfNodePropVO getSelfNodeProp(){
		List<DataxSelfNodePropVO> dataxSelfNodePropVOlist = findAll();
		DataxSelfNodePropVO dataxSelfNodePropVO = null;
		if (!dataxSelfNodePropVOlist.isEmpty()) {
			dataxSelfNodePropVO = dataxSelfNodePropVOlist
					.get(0);
		}
		return dataxSelfNodePropVO;
	}

}
